using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;

namespace AltNetHispano.Vale
{
	public static class ValidatorExtensions
	{
		public static void Register<T>(this IValidator validator, Func<T, string> assert, Expression<Func<T, object>> property)
		{
			MemberInfo member = TypeUtils.DecodeMemberAccessExpression(property);
			validator.Register(assert, member);
		}

		public static IEnumerable<string> GetInvalidMessages<T>(this IValidator validator, T entity, Expression<Func<T, object>> property)
		{
			MemberInfo member = TypeUtils.DecodeMemberAccessExpression(property);
			return validator.GetInvalidMessages(entity, member);
		}

		public static IEnumerable<string> GetInvalidMessages<T>(this IValidator validator, T entity, string propertyName)
		{
			var property = entity.GetType().GetProperty(propertyName);
			//TODO too much reflection
			if(property == null)
			{
				throw new ArgumentException(string.Format("{0} is not a property", propertyName));
			}
			return validator.GetInvalidMessages(entity, property);
		}
	}
}